import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.JTabbedPane;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Font;



public class PropertySelectionUI extends JFrame implements CountInterface{

	private JPanel contentPane, panel, panel1;
	private JLabel largeDeed, largeDeedSelected, ownerLbl, numOfHomesLbl;
	private Player p;
	private Block[] block;
	private Block lastActiveBlock;
	private PropertyBlock[] pb;
	private PropertyBlock[] pbGroup;
	private PropertyBlock activeProp;
	private Utilities[] util;
	private Utilities activeUtil;
	private RailRoad[] rail;
	private RailRoad activeRail;
	private JButton[] btn;
	private JButton buildHouseBtn, sellHouseBtn, returnBtn, mortgageBtn, unMortgageBtn;
	private ImageIcon[] imgIcon;
	private ImageIcon largeDeedIcon;
	private ImageIcon largeMortgageIcon;
	private int btnCount;
	private JLabel cOwnerLbl;
	private JLabel cNumOfHomesLbl;
	private JLabel lblCPlayerMoney, lblCurrentNumberOf, lblCurrentOwner;
	private JLabel cPlayerMoneyLbl;

	
	public PropertySelectionUI(Player player, Block[] blk){
		
		this.p = player;
		this.block = blk;
		
		this.util = new Utilities[iUtilCount];
		this.rail = new RailRoad[iRailCount];
		this.pb = new PropertyBlock[iPropCount];
		
		this.btn = new JButton[(iUtilCount+iRailCount+iPropCount)];
		this.imgIcon = new ImageIcon[(iUtilCount+iRailCount+iPropCount)];
		this.btnCount = 0;
		
		fillBlockArrays();
		
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(71, 127, 658, 390);
		contentPane = new JPanel();
		contentPane.setLayout(null);
		setContentPane(contentPane);
		this.setVisible(true);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(10, 11, 355, 311);
		contentPane.add(tabbedPane);
		
		panel = new JPanel();
		panel.setLayout(null);
		panel.setPreferredSize(new Dimension(340,900));
		
		panel1 = new JPanel();
		panel1.setLayout(null);
		panel1.setPreferredSize(new Dimension(340,900));
		
		fillOwnedPropertyTab();
		System.out.println(btnCount+"completely after all owned property");
		fillUnOwnedPropertyTab();
		
		JScrollPane scrollPane = new JScrollPane(panel,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		tabbedPane.addTab("Owned Property", null, scrollPane, null);
		
		JScrollPane scrollPane_1 = new JScrollPane(panel1,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		tabbedPane.addTab("Property of Others", null, scrollPane_1, null);
			
		ImageIcon initialIcon = new ImageIcon("Graphics/defcard.png");
		largeDeed = new JLabel(initialIcon);
		largeDeed.setBounds(418, 41, 170, 200);
		contentPane.add(largeDeed);
		
		this.buildHouseBtn = new JButton("Build A House");
		this.buildHouseBtn.setBounds(10,333,150,20);
		this.buildHouseBtn.setVisible(false);
        this.buildHouseBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buildHouseEvent(evt);
            }
        });
		contentPane.add(buildHouseBtn);
		
		this.sellHouseBtn = new JButton("Sell A House");
		this.sellHouseBtn.setBounds(170,333,150,20);
		this.sellHouseBtn.setVisible(false);
        this.sellHouseBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sellHouseEvent(evt);
            }
        });
		contentPane.add(sellHouseBtn);
		
		this.returnBtn = new JButton("Return to Game");
		this.returnBtn.setBounds(490,333,150,20);
		this.returnBtn.setVisible(true);
        this.returnBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                returnEvent(evt);
            }
        });
		contentPane.add(returnBtn);
		
		this.mortgageBtn = new JButton("Mortgage Property");
		this.mortgageBtn.setBounds(330,333,150,20);
		this.mortgageBtn.setVisible(false);
	    this.mortgageBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mortgageEvent(evt);
            }
        });
		contentPane.add(mortgageBtn);
		
		this.unMortgageBtn = new JButton("Unmortgage Property");
		this.unMortgageBtn.setBounds(330,333,150,20);
		this.unMortgageBtn.setVisible(false);
	    this.unMortgageBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                unMortgageEvent(evt);
            }
        });
		contentPane.add(unMortgageBtn);
		
		lblCurrentOwner = new JLabel("Current Owner:");
		lblCurrentOwner.setFont(new Font("Tahoma", Font.PLAIN, 9));
		lblCurrentOwner.setBounds(375, 259, 107, 14);
		lblCurrentOwner.setVisible(true);
		contentPane.add(lblCurrentOwner);
		
		lblCPlayerMoney = new JLabel("Your Total Money:");
		lblCPlayerMoney.setFont(new Font("Tahoma", Font.PLAIN, 9));
		lblCPlayerMoney.setBounds(375, 16, 124, 14);
		lblCPlayerMoney.setVisible(true);
		contentPane.add(lblCPlayerMoney);
		
		lblCurrentNumberOf = new JLabel("Current Number of Homes:");
		lblCurrentNumberOf.setFont(new Font("Tahoma", Font.PLAIN, 9));
		lblCurrentNumberOf.setBounds(375, 280, 124, 14);
		lblCurrentNumberOf.setVisible(false);
		contentPane.add(lblCurrentNumberOf);
		
		cOwnerLbl = new JLabel("");
		cOwnerLbl.setFont(new Font("Tahoma", Font.BOLD, 9));
		cOwnerLbl.setBounds(509, 259, 132, 14);
		cOwnerLbl.setVisible(false);
		contentPane.add(cOwnerLbl);
		
		cPlayerMoneyLbl = new JLabel(Integer.toString(this.p.getCurrentMoney()));
		cPlayerMoneyLbl.setFont(new Font("Tahoma", Font.BOLD, 9));
		cPlayerMoneyLbl.setBounds(509, 16, 113, 14);
		cPlayerMoneyLbl.setVisible(true);
		contentPane.add(cPlayerMoneyLbl);
		
		cNumOfHomesLbl = new JLabel("");
		cNumOfHomesLbl.setFont(new Font("Tahoma", Font.BOLD, 9));
		cNumOfHomesLbl.setBounds(509, 297, 46, 14);
		cNumOfHomesLbl.setVisible(false);
		contentPane.add(cNumOfHomesLbl);
			
		scrollPane.setVisible(true);
		scrollPane_1.setVisible(true);
		
	}
	
	public void buildHouseEvent(java.awt.event.ActionEvent evt){
		int counter = 0;
		int tempGroupNum = 0;
		int tempCompareCounter = 0;
		int tempMortgageCounter = 0;
		boolean buildable = false;
		
		if(this.activeProp!=null){
			for(int i= 0; i<this.pb.length; i++){//this for loop counts all the properties that belong to the active property group
				if(this.activeProp.getPropertyGroup()==this.pb[i].getPropertyGroup()){
					counter++;
				}
			}
			this.pbGroup = new PropertyBlock[counter];
			
			for(int i=0; i<this.pb.length; i++){//this loop adds all the properties that belong to the active property group

				if(this.activeProp.getPropertyGroup()==this.pb[i].getPropertyGroup()){
					this.pbGroup[tempGroupNum]=this.pb[i];
					System.out.println(pbGroup[tempGroupNum].getBlockNumber()+" from abcd");
					tempGroupNum++;
				}
			}


			for(int i = 0; i<this.pbGroup.length; i++){//this loop counts how many properties the active player owns in the active property group
				if(this.pbGroup[i].getCOwner()!=null){
					if(this.activeProp.getCOwner()==this.pbGroup[i].getCOwner()){
						tempCompareCounter++;
					}
				}	
			}
			
			for(int i = 0; i<this.pbGroup.length; i++){//this loop checks to see if any properties in the group are mortgaged
				if(this.pbGroup[i].getCOwner()!=null){
					if(this.pbGroup[i].getIsMortgaged()==true){
						tempMortgageCounter++;
					}
				}	
			}
			
			if(this.pbGroup.length==tempCompareCounter){//this if checks to see if you own all the properties in that group before putting up a home
				if(this.pbGroup.length==2){//Compares current number of homes and checks eligibility to build a house (based on 2 properties block)
					if(tempMortgageCounter<=0){
						if(this.activeProp.getCurrentNumberOfHouses()<=this.pbGroup[0].getCurrentNumberOfHouses() && this.activeProp.getCurrentNumberOfHouses()<=this.pbGroup[1].getCurrentNumberOfHouses()){
							buildable=true;
						}else{
							JOptionPane.showMessageDialog(null, "Hoy, kailangan mo mag tayo ng bahay proportionally sa mga properties mo!");
						}
					}else{
						JOptionPane.showMessageDialog(null, "This or one of the properites that belong to the group are Mortgaged. Remove the mortgage before building homes");
					}	
				}else if(pbGroup.length==3){//Compares current number of homes and checks eligibility to build a house (based on 3 properties block)
					if(tempMortgageCounter<=0){	
						if(this.activeProp.getCurrentNumberOfHouses()<=this.pbGroup[0].getCurrentNumberOfHouses() && this.activeProp.getCurrentNumberOfHouses()<=this.pbGroup[1].getCurrentNumberOfHouses() &&this.activeProp.getCurrentNumberOfHouses()<=this.pbGroup[2].getCurrentNumberOfHouses()){
							buildable=true;
						}else{
							JOptionPane.showMessageDialog(null, "Hoy, kailangan mo mag tayo ng bahay proportionally sa mga properties mo!");
						}
					}else{
						JOptionPane.showMessageDialog(null, "This or one of the properites that belong to the group are Mortgaged. Remove the mortgage before building homes");
					}	
				}
				
				if(buildable==true){//this actually adds a house on the active property block
					if(this.activeProp.getCurrentNumberOfHouses()<6){//checks to see if you're a moron
						if(this.p.getCurrentMoney()-this.activeProp.getCostOfHouse()>=0){
							this.p.subtractMoney(this.activeProp.getCostOfHouse());
							this.activeProp.setCurrentNumberOfHouses(this.activeProp.getCurrentNumberOfHouses()+1);
							new Sound().play("Sounds/houseBuild.wav");
							if(this.activeProp.getCurrentNumberOfHouses()<5){
								JOptionPane.showMessageDialog(null, "Kongrats! Meron ka ng "+this.activeProp.getCurrentNumberOfHouses()+" na bahay sa property mo!");
							}else{
								JOptionPane.showMessageDialog(null, "Kongrats! Meron ka ng Hotel sa property mo!");
							}
							
							updateMoneyLbl();
							updateHouseLbl();
							//this is where we call the interface CommandWithInt
							CommandWithInt cmd = new DisplayHouseTokenCommand();
							cmd.execute(this.activeProp.getBlockNumber());
							
						}else{
							JOptionPane.showMessageDialog(null, "Pucha, hindi kasya ang pera mo! Bumili ka na lang ng fish balls.");
						}
					}else{
						JOptionPane.showMessageDialog(null, "Grabe! Ang bobo mo talaga, hindi ka pwedeng tayo ng isa pang hotel!");
					}
				}
			}else{
				JOptionPane.showMessageDialog(null, "Tanga, hindi mo pala pag mamay-ari ang lahat ng properties sa groupo na yan!");
			}	
		}else{
			JOptionPane.showMessageDialog(null, "Tanga, mag select ka muna ng isang property!");
		}
	}
	
	public void sellHouseEvent(java.awt.event.ActionEvent evt){
		int counter = 0;
		int tempGroupNum = 0;
		int tempCompareCounter = 0;
		boolean buildable = false;
		
		if(this.activeProp!=null){
			for(int i= 0; i<this.pb.length; i++){//this for loop counts all the properties that belong to the active property group
				if(this.activeProp.getPropertyGroup()==this.pb[i].getPropertyGroup()){
					counter++;
				}
			}
			this.pbGroup = new PropertyBlock[counter];
			
			for(int i=0; i<this.pb.length; i++){//this loop adds all the properties that belong to the active property group
				System.out.println(this.activeProp.getBlockNumber()+"this is the active block num");
				System.out.println(this.activeProp.getPropertyGroup()+"this is the prop group to compare");

				if(this.activeProp.getPropertyGroup()==this.pb[i].getPropertyGroup()){
					this.pbGroup[tempGroupNum]=this.pb[i];
					System.out.println(pbGroup[tempGroupNum].getBlockNumber()+" from abcd");
					tempGroupNum++;
				}
			}


			for(int i = 0; i<this.pbGroup.length; i++){//this loop counts how many properties the active player owns in the active property group
				if(this.pbGroup[i].getCOwner()!=null){
					if(this.activeProp.getCOwner()==this.pbGroup[i].getCOwner()){
						tempCompareCounter++;
					}
				}
				
			}
			
			if(this.pbGroup.length==tempCompareCounter){//this if checks to see if you own all the properties in that group before putting up a home
				if(this.pbGroup.length==2){//Compares current number of homes and checks eligibility to build a house (based on 2 properties block)
					if(this.activeProp.getCurrentNumberOfHouses()>=this.pbGroup[0].getCurrentNumberOfHouses() && this.activeProp.getCurrentNumberOfHouses()>=this.pbGroup[1].getCurrentNumberOfHouses()){
						buildable=true;
					}else{
						JOptionPane.showMessageDialog(null, "Hoy, kailangan mo mag tayo ng bahay proportionally sa mga properties mo!");
					}
				}else if(pbGroup.length==3){//Compares current number of homes and checks eligibility to build a house (based on 3 properties block)
					if(this.activeProp.getCurrentNumberOfHouses()>=this.pbGroup[0].getCurrentNumberOfHouses() && this.activeProp.getCurrentNumberOfHouses()>=this.pbGroup[1].getCurrentNumberOfHouses() &&this.activeProp.getCurrentNumberOfHouses()>=this.pbGroup[2].getCurrentNumberOfHouses()){
						buildable=true;
					}else{
						JOptionPane.showMessageDialog(null, "Hoy, kailangan mo mag tayo ng bahay proportionally sa mga properties mo!");
					}
				}
				
				if(buildable==true){//this actually adds a house on the active property block
					if(this.activeProp.getCurrentNumberOfHouses()>0){//checks to see if you're a moron
			
						this.p.addMoney((this.activeProp.getCostOfHouse()/2));
						this.activeProp.setCurrentNumberOfHouses(this.activeProp.getCurrentNumberOfHouses()-1);
						new Sound().play("Sounds/houseSell.wav");
						JOptionPane.showMessageDialog(null, "Na benta na ang isang bahay mo. Kawawa ka naman");
						
						updateMoneyLbl();
						updateHouseLbl();
						//this is where we call the interface CommandWithInt
						CommandWithInt cmd = new DisplayHouseTokenCommand();
						cmd.execute(this.activeProp.getBlockNumber());
						
					}else{
						JOptionPane.showMessageDialog(null, "Grabe! Ang bobo mo talaga, wala ka naman bahay sa property na yan!");
					}
				}
			}else{
				JOptionPane.showMessageDialog(null, "Tanga, hindi mo pala pag mamay-ari ang lahat ng properties sa groupo na yan!");
			}	
		}else{
			JOptionPane.showMessageDialog(null, "Tanga, mag select ka muna ng isang property!");
		}
	}
	
	public void returnEvent(java.awt.event.ActionEvent evt){
		this.dispose();
	}
	
	public void mortgageEvent(java.awt.event.ActionEvent evt){
		int counter = 0;
		int tempGroupNum = 0;
		int tempCompareCounter = 0;
		boolean buildable = false;
		
		if(this.lastActiveBlock!=null){
			if(this.lastActiveBlock instanceof PropertyBlock){
				this.activeProp = (PropertyBlock)lastActiveBlock;
				
				for(int i= 0; i<this.pb.length; i++){//this for loop counts all the properties that belong to the active property group
					if(this.activeProp.getPropertyGroup()==this.pb[i].getPropertyGroup()){
						counter++;
					}
				}
				this.pbGroup = new PropertyBlock[counter];
				
				for(int i=0; i<this.pb.length; i++){//this loop adds all the properties that belong to the active property group

					if(this.activeProp.getPropertyGroup()==this.pb[i].getPropertyGroup()){
						this.pbGroup[tempGroupNum]=this.pb[i];
						tempGroupNum++;
					}
				}
				
				if(this.pbGroup.length==2){//Compares current number of homes and checks eligibility to build a house (based on 2 properties block)
					if(this.pbGroup[0].getCurrentNumberOfHouses()>0 || this.pbGroup[1].getCurrentNumberOfHouses()>0){
						String message = "There are houses/hotels on this property group. Click 'Yes' to sell all houses/hotels before mortgaging this property or 'No' to cancel";
					    String title = "Really Sell All Homes/Hotels And Mortgage Property?";
					    
					    int reply = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION);
					    
					    if(reply == JOptionPane.YES_OPTION){
					    	this.p.addMoney((pbGroup[0].getCurrentNumberOfHouses()+pbGroup[1].getCurrentNumberOfHouses())*(this.activeProp.getCostOfHouse()/2)+this.activeProp.getMortgageValue());
					    	pbGroup[0].setCurrentNumberOfHouses(0);
					    	pbGroup[1].setCurrentNumberOfHouses(0);
					    	this.activeProp.setIsMortgaged(true);
							
							this.largeDeedSelected.setVisible(false);
							this.largeDeedSelected = new JLabel(largeMortgageIcon);

							updateLargeDeedSelectedLbl();
							updateMoneyLbl();
							updateHouseLbl();
							
							JOptionPane.showMessageDialog(null, "Your property has been mortgaged! You have received P"+this.activeProp.getMortgageValue()+".");
							
							this.buildHouseBtn.setVisible(false);
							this.sellHouseBtn.setVisible(false);
							this.mortgageBtn.setVisible(false);
							this.unMortgageBtn.setVisible(true);
							
							CommandWithInt cmd = new DisplayHouseTokenCommand();
							cmd.execute(this.pbGroup[0].getBlockNumber());
							cmd.execute(this.pbGroup[1].getBlockNumber());
					    }
					}else{
						this.activeProp.setIsMortgaged(true);
						this.p.addMoney(this.activeProp.getMortgageValue());
						
						this.largeDeedSelected.setVisible(false);
						this.largeDeedSelected = new JLabel(largeMortgageIcon);

						updateLargeDeedSelectedLbl();
						updateMoneyLbl();
						
						JOptionPane.showMessageDialog(null, "Your property has been mortgaged! You have received P"+this.activeProp.getMortgageValue()+".");
						
						this.buildHouseBtn.setVisible(false);
						this.sellHouseBtn.setVisible(false);
						this.mortgageBtn.setVisible(false);
						this.unMortgageBtn.setVisible(true);
					}
				}else if(pbGroup.length==3){//Compares current number of homes and checks eligibility to build a house (based on 3 properties block)
					if(this.pbGroup[0].getCurrentNumberOfHouses()>0 || this.pbGroup[1].getCurrentNumberOfHouses()>0 || this.pbGroup[2].getCurrentNumberOfHouses()>0){
						String message = "There are houses/hotels on this property group. Click 'Yes' to sell all houses/hotels and mortgage the property or 'No' to cancel";
					    String title = "Really Sell All Homes/Hotels And Mortgage Property?";
					    
					    int reply = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION);
					    
					    if(reply == JOptionPane.YES_OPTION){
					    	this.p.addMoney((pbGroup[0].getCurrentNumberOfHouses()+pbGroup[1].getCurrentNumberOfHouses()+pbGroup[2].getCurrentNumberOfHouses())*(this.activeProp.getCostOfHouse()/2)+this.activeProp.getMortgageValue());
					    	pbGroup[0].setCurrentNumberOfHouses(0);
					    	pbGroup[1].setCurrentNumberOfHouses(0);
					    	pbGroup[2].setCurrentNumberOfHouses(0);
					    	this.activeProp.setIsMortgaged(true);
							
							this.largeDeedSelected.setVisible(false);
							this.largeDeedSelected = new JLabel(largeMortgageIcon);

							updateLargeDeedSelectedLbl();
							updateMoneyLbl();
							updateHouseLbl();
							
							JOptionPane.showMessageDialog(null, "Your property has been mortgaged! You have received P"+this.activeProp.getMortgageValue()+".");
							
							this.buildHouseBtn.setVisible(false);
							this.sellHouseBtn.setVisible(false);
							this.mortgageBtn.setVisible(false);
							this.unMortgageBtn.setVisible(true);
							
							CommandWithInt cmd = new DisplayHouseTokenCommand();
							cmd.execute(this.pbGroup[0].getBlockNumber());
							cmd.execute(this.pbGroup[1].getBlockNumber());
							cmd.execute(this.pbGroup[2].getBlockNumber());
					    }
					}else{
						this.activeProp.setIsMortgaged(true);
						this.p.addMoney(this.activeProp.getMortgageValue());
						
						this.largeDeedSelected.setVisible(false);
						this.largeDeedSelected = new JLabel(largeMortgageIcon);
						
						updateLargeDeedSelectedLbl();
						updateMoneyLbl();
						
						JOptionPane.showMessageDialog(null, "Your property has been mortgaged! You have received P"+this.activeProp.getMortgageValue()+".");
						
						this.buildHouseBtn.setVisible(false);
						this.sellHouseBtn.setVisible(false);
						this.mortgageBtn.setVisible(false);
						this.unMortgageBtn.setVisible(true);
						
					}
				}	
				
			}else if(this.lastActiveBlock instanceof Utilities){
				this.activeUtil = (Utilities)lastActiveBlock;
				this.activeUtil.setIsMortgaged(true);
				this.p.addMoney(this.activeUtil.getMortgageValue());
				
				this.largeDeedSelected.setVisible(false);
				this.largeDeedSelected = new JLabel(largeMortgageIcon);

				updateLargeDeedSelectedLbl();
				updateMoneyLbl();
				
				JOptionPane.showMessageDialog(null, "Your property has been mortgaged! You have received P"+(this.activeUtil.getMortgageValue())+".");
				
				this.buildHouseBtn.setVisible(false);
				this.sellHouseBtn.setVisible(false);
				this.mortgageBtn.setVisible(false);
				this.unMortgageBtn.setVisible(true);
				
			}else if(this.lastActiveBlock instanceof RailRoad){
				this.activeRail = (RailRoad)lastActiveBlock;
				this.activeRail.setIsMortgaged(true);
				
				this.p.addMoney(this.activeRail.getMortgageValue());
				
				this.largeDeedSelected.setVisible(false);
				this.largeDeedSelected = new JLabel(largeMortgageIcon);
				
				updateLargeDeedSelectedLbl();
				updateMoneyLbl();
				
				JOptionPane.showMessageDialog(null, "Your property has been mortgaged! You have received P"+(this.activeRail.getMortgageValue())+".");
				
				this.buildHouseBtn.setVisible(false);
				this.sellHouseBtn.setVisible(false);
				this.mortgageBtn.setVisible(false);
				this.unMortgageBtn.setVisible(true);
			}
		}else{
			JOptionPane.showMessageDialog(null, "Tanga, mag select ka muna ng isang property!");
		}
	}
	
	public void unMortgageEvent(java.awt.event.ActionEvent evt){
		if(this.lastActiveBlock!=null){
			if(this.lastActiveBlock instanceof PropertyBlock){
				this.activeProp = (PropertyBlock)lastActiveBlock;
				
				if(this.activeProp.getIsMortgaged()==true){
					this.activeProp.setIsMortgaged(false);
					this.p.subtractMoney(this.activeProp.getMortgageValue()+(int)(this.activeProp.getMortgageValue()*.1));
					
					this.largeDeedSelected.setVisible(false);
					this.largeDeedSelected = new JLabel(largeDeedIcon);
					
					updateLargeDeedSelectedLbl();
					updateMoneyLbl();
					
					JOptionPane.showMessageDialog(null, "Your property has been unmortgaged! You have spent P"+(this.activeProp.getMortgageValue()+(int)(this.activeProp.getMortgageValue()*.1))+".");
					
					this.buildHouseBtn.setVisible(true);
					this.sellHouseBtn.setVisible(true);
					this.mortgageBtn.setVisible(true);
					this.unMortgageBtn.setVisible(false);
				}
			}else if(this.lastActiveBlock instanceof Utilities){
				if(this.activeUtil.getIsMortgaged()==true){
					this.activeUtil = (Utilities)lastActiveBlock;
					this.activeUtil.setIsMortgaged(false);
					this.p.subtractMoney(this.activeUtil.getMortgageValue()+(int)(this.activeUtil.getMortgageValue()*.1));
					
					this.largeDeedSelected.setVisible(false);
					this.largeDeedSelected = new JLabel(largeDeedIcon);

					updateLargeDeedSelectedLbl();
					updateMoneyLbl();
					
					JOptionPane.showMessageDialog(null, "Your property has been unmortgaged! You have spent P"+(this.activeUtil.getMortgageValue()+(int)(this.activeUtil.getMortgageValue()*.1))+".");
					
					this.buildHouseBtn.setVisible(true);
					this.sellHouseBtn.setVisible(true);
					this.mortgageBtn.setVisible(true);
					this.unMortgageBtn.setVisible(false);
				}
			}else if(this.lastActiveBlock instanceof RailRoad){
				if(this.activeRail.getIsMortgaged()==true){
					this.activeRail = (RailRoad)lastActiveBlock;
					this.activeRail.setIsMortgaged(false);
					this.p.subtractMoney(this.activeRail.getMortgageValue()+(int)(this.activeRail.getMortgageValue()*.1));
					
					this.largeDeedSelected.setVisible(false);
					this.largeDeedSelected = new JLabel(largeDeedIcon);
					
					updateLargeDeedSelectedLbl();
					updateMoneyLbl();
					
					JOptionPane.showMessageDialog(null, "Your property has been unmortgaged! You have spent P"+(this.activeRail.getMortgageValue()+(int)(this.activeRail.getMortgageValue()*.1))+".");
					
					this.buildHouseBtn.setVisible(true);
					this.sellHouseBtn.setVisible(true);
					this.mortgageBtn.setVisible(true);
					this.unMortgageBtn.setVisible(false);
				}
			}
		}else{
			JOptionPane.showMessageDialog(null, "Tanga, mag select ka muna ng isang property!");
		}
	}
	
	public void fillBlockArrays(){
		int tempPropBlockNum = 0;
		int tempRailRoadNum = 0;
		int tempUtilityNum = 0;
		
		for(int i=0;i<this.block.length;i++){
			if(block[i] instanceof Utilities){
				util[tempUtilityNum] = (Utilities)block[i];
				tempUtilityNum++;
			}else if(block[i] instanceof RailRoad){
				rail[tempRailRoadNum] = (RailRoad)block[i];
				tempRailRoadNum++;
			}else if(block[i] instanceof PropertyBlock){
				pb[tempPropBlockNum] = (PropertyBlock)block[i];
				tempPropBlockNum++;
			}
		}
	}
	
	public void fillOwnedPropertyTab(){
		int x = 10;
		int x1 = 10;
		int y = 10;
		int y1 = 10;
		
		DisplayLargeRailDeed displayLrgRailDeed;
		DisplayLargeUtilDeed displayLrgUtilDeed;
		DisplayLargePropDeed displayLrgPropDeed;
		for(int i = 0 ; i<this.pb.length ; i++){
			if(this.pb[i].getCOwner()==this.p){
					this.imgIcon[i] = new ImageIcon(this.pb[i].getPropertyDeedSmall());
					this.btn[this.btnCount] = new JButton(this.imgIcon[i]);
					this.btn[this.btnCount].setBounds(x,y,68,80);
					this.btn[this.btnCount].setVisible(true);
					this.btn[this.btnCount].addActionListener(displayLrgPropDeed = new DisplayLargePropDeed(this.pb[i]));
			        panel.add(btn[this.btnCount]);
			        this.btnCount++;
						
					x=x+75;
					
					if(x>=300){
						x=10;
						y=y+95;
					}
					System.out.println(btnCount+"after filling Owned");
			}	
		}//end of property for loop
		
		for(int i = 0 ; i<this.util.length ; i++){
			if(this.util[i].getCOwner()==this.p){
					this.imgIcon[i] = new ImageIcon(this.util[i].getPropertyDeedSmall());
					this.btn[this.btnCount] = new JButton(this.imgIcon[i]);
					this.btn[this.btnCount].setBounds(x,y,68,80);
					this.btn[this.btnCount].setVisible(true);
					this.btn[this.btnCount].addActionListener(displayLrgUtilDeed = new DisplayLargeUtilDeed(this.util[i]));
			        panel.add(btn[this.btnCount]);
			        this.btnCount++;
		
					x=x+75;
					
					if(x>=300){
						x=10;
						y=y+95;
					}
			}
		}//end of util for loop
		
		for(int i = 0 ; i<this.rail.length ; i++){
			if(this.rail[i].getCOwner()==this.p){
					this.imgIcon[i] = new ImageIcon(this.rail[i].getPropertyDeedSmall());
					this.btn[this.btnCount] = new JButton(this.imgIcon[i]);
					this.btn[this.btnCount].setBounds(x,y,68,80);
					this.btn[this.btnCount].setVisible(true);
			        this.btn[this.btnCount].addActionListener(displayLrgRailDeed = new DisplayLargeRailDeed(this.rail[i]));
			        panel.add(btn[this.btnCount]);
			        this.btnCount++;
		
					x=x+75;
					
					if(x>=300){
						x=10;
						y=y+95;
					}
				
			}
		}//end of rail for loop
	
	}//end of method
	
	public void fillUnOwnedPropertyTab(){
		int x = 10;
		int y = 10;
		
		DisplayLargeRailDeed displayLrgRailDeed;
		DisplayLargeUtilDeed displayLrgUtilDeed;
		DisplayLargePropDeed displayLrgPropDeed;
		
		for(int i = 0 ; i<this.pb.length ; i++){
			if(this.pb[i].getCOwner()==null){
					this.imgIcon[i] = new ImageIcon(this.pb[i].getPropertyDeedSmall());
					this.btn[this.btnCount] = new JButton(this.imgIcon[i]);
					this.btn[this.btnCount].setBounds(x,y,68,80);
					this.btn[this.btnCount].setToolTipText("Available for Sale");
					this.btn[this.btnCount].setVisible(true);
					this.btn[this.btnCount].addActionListener(displayLrgPropDeed = new DisplayLargePropDeed(this.pb[i]));
			        panel1.add(btn[this.btnCount]);
			        this.btnCount++;
						
					x=x+75;
					
					if(x>=300){
						x=10;
						y=y+95;
					}
			}else if(this.pb[i].getCOwner()!=this.p){
					this.imgIcon[i] = new ImageIcon(this.pb[i].getPropertyDeedSmall());
					this.btn[this.btnCount] = new JButton(this.imgIcon[i]);
					this.btn[this.btnCount].setBounds(x,y,68,80);
					this.btn[this.btnCount].setToolTipText(this.pb[i].getCOwner().toString());
					this.btn[this.btnCount].setVisible(true);
					this.btn[this.btnCount].addActionListener(displayLrgPropDeed = new DisplayLargePropDeed(this.pb[i]));
			        panel1.add(btn[this.btnCount]);
			        this.btnCount++;
						
					x=x+75;
					
					if(x>=300){
						x=10;
						y=y+95;
					}
			}	
		}//end of property for loop
		
		for(int i = 0 ; i<this.util.length ; i++){
				if(this.util[i].getCOwner()==null){
					this.imgIcon[i] = new ImageIcon(this.util[i].getPropertyDeedSmall());
					this.btn[this.btnCount] = new JButton(this.imgIcon[i]);
					this.btn[this.btnCount].setBounds(x,y,68,80);
					this.btn[this.btnCount].setToolTipText("Available for Sale");
					this.btn[this.btnCount].setVisible(true);
					this.btn[this.btnCount].addActionListener(displayLrgUtilDeed = new DisplayLargeUtilDeed(this.util[i]));
			        panel1.add(btn[this.btnCount]);
			        this.btnCount++;
		
					x=x+75;
					
					if(x>=300){
						x=10;
						y=y+95;
					}
				}else if(this.util[i].getCOwner()!=this.p){
					this.imgIcon[i] = new ImageIcon(this.util[i].getPropertyDeedSmall());
					this.btn[this.btnCount] = new JButton(this.imgIcon[i]);
					this.btn[this.btnCount].setBounds(x,y,68,80);
					this.btn[this.btnCount].setToolTipText(this.util[i].getCOwner().toString());
					this.btn[this.btnCount].setVisible(true);
					this.btn[this.btnCount].addActionListener(displayLrgUtilDeed = new DisplayLargeUtilDeed(this.util[i]));
			        panel1.add(btn[this.btnCount]);
			        this.btnCount++;
						
					x=x+75;
					
					if(x>=300){
						x=10;
						y=y+95;
					}
			}
		}//end of util for loop
		
		for(int i = 0 ; i<this.rail.length ; i++){
			if(this.rail[i].getCOwner()==null){
					this.imgIcon[i] = new ImageIcon(this.rail[i].getPropertyDeedSmall());
					this.btn[this.btnCount] = new JButton(this.imgIcon[i]);
					this.btn[this.btnCount].setBounds(x,y,68,80);
					this.btn[this.btnCount].setToolTipText("Available for Sale");
					this.btn[this.btnCount].setVisible(true);
			        this.btn[this.btnCount].addActionListener(displayLrgRailDeed = new DisplayLargeRailDeed(this.rail[i]));
			        panel1.add(btn[this.btnCount]);
			        this.btnCount++;
		
					x=x+75;
					
					if(x>=300){
						x=10;
						y=y+95;
					}
			}else if(this.rail[i].getCOwner()!=this.p){
				this.imgIcon[i] = new ImageIcon(this.rail[i].getPropertyDeedSmall());
				this.btn[this.btnCount] = new JButton(this.imgIcon[i]);
				this.btn[this.btnCount].setBounds(x,y,68,80);
				this.btn[this.btnCount].setToolTipText(this.rail[i].getCOwner().toString());
				this.btn[this.btnCount].setVisible(true);
				this.btn[this.btnCount].addActionListener(displayLrgRailDeed = new DisplayLargeRailDeed(this.rail[i]));
		        panel1.add(btn[this.btnCount]);
		        this.btnCount++;
					
				x=x+75;
				
				if(x>=300){
					x=10;
					y=y+95;
				}
			}
		}//end of rail for loop
	
	}
	
	public void updateMoneyLbl(){
		this.cPlayerMoneyLbl.setVisible(false);
		this.cPlayerMoneyLbl = new JLabel(Integer.toString(this.p.getCurrentMoney()));
		this.cPlayerMoneyLbl.setFont(new Font("Tahoma", Font.BOLD, 9));
		this.cPlayerMoneyLbl.setBounds(509, 16, 113, 14);
		this.cPlayerMoneyLbl.setVisible(true);
		this.contentPane.add(cPlayerMoneyLbl);
	}
	
	public void updateHouseLbl(){
		this.cNumOfHomesLbl.setVisible(false);
		this.cNumOfHomesLbl = new JLabel(Integer.toString(activeProp.getCurrentNumberOfHouses()));
		this.cNumOfHomesLbl.setFont(new Font("Tahoma", Font.BOLD, 9));
		this.cNumOfHomesLbl.setBounds(509, 280, 46, 14);
		this.cNumOfHomesLbl.setVisible(true);
		this.contentPane.add(cNumOfHomesLbl);
	}
	
	public void updateOwnerLbl(){
		cOwnerLbl.setFont(new Font("Tahoma", Font.BOLD, 9));
		cOwnerLbl.setBounds(509, 259, 150, 14);
		cOwnerLbl.setVisible(true);
		contentPane.add(cOwnerLbl);
	}
	
	public void updateLargeDeedSelectedLbl(){
		largeDeedSelected.setBounds(426, 33, 170, 200);
		largeDeedSelected.setVisible(true);
		contentPane.add(largeDeedSelected);
	}
	
	public void setButtonsAndLabelsInvisible(){
		largeDeed.setVisible(false);
		cOwnerLbl.setVisible(false);
		cPlayerMoneyLbl.setVisible(false);
		cNumOfHomesLbl.setVisible(false);
		buildHouseBtn.setVisible(false);
		sellHouseBtn.setVisible(false);
		mortgageBtn.setVisible(false);
		unMortgageBtn.setVisible(false);
		
	}
	
	private class DisplayLargeRailDeed implements ActionListener {
		RailRoad activeRailRoad;
		
		public DisplayLargeRailDeed(RailRoad railrd){
			activeRailRoad = railrd;
		}
		
		public void actionPerformed(ActionEvent e){
			setButtonsAndLabelsInvisible();
			
			if(largeDeedSelected!=null){
				largeDeedSelected.setVisible(false);
				contentPane.remove(largeDeedSelected);
			}			

			largeDeedIcon = new ImageIcon(this.activeRailRoad.getPropertyDeedLarge());
			largeMortgageIcon = new ImageIcon(this.activeRailRoad.getPropertyDeedMortgaged());
			activeRail = this.activeRailRoad;
			lastActiveBlock=(Block)activeRail;
			lblCurrentNumberOf.setVisible(false);
			
			if(activeRailRoad.getCOwner()==null){
				cOwnerLbl = new JLabel("The Bank");
			}else{
				cOwnerLbl = new JLabel(activeRailRoad.getCOwner().getPlayerName());
			}
			
			updateOwnerLbl();
			updateMoneyLbl();
			
			if(activeRail.getIsMortgaged()==false){
				if(activeRail.getCOwner()==p){
					mortgageBtn.setVisible(true);
				}else{
					mortgageBtn.setVisible(false);
					unMortgageBtn.setVisible(false);
				}
				
				largeDeedSelected = new JLabel(largeDeedIcon);
			}else{
				if(activeRail.getCOwner()==p){
					unMortgageBtn.setVisible(true);
				}else{
					mortgageBtn.setVisible(false);
					unMortgageBtn.setVisible(false);
				}
				largeDeedSelected = new JLabel(largeMortgageIcon);;
			}
			updateLargeDeedSelectedLbl();
		}
	}
	
	private class DisplayLargeUtilDeed implements ActionListener {
		Utilities activeUtility;

		public DisplayLargeUtilDeed(Utilities utl){
			activeUtility = utl;
		}
		
		public void actionPerformed(ActionEvent e){
			setButtonsAndLabelsInvisible();
			
			if(largeDeedSelected!=null){
				largeDeedSelected.setVisible(false);
				contentPane.remove(largeDeedSelected);
			}			

			largeDeedIcon = new ImageIcon(this.activeUtility.getPropertyDeedLarge());
			largeMortgageIcon = new ImageIcon(this.activeUtility.getPropertyDeedMortgaged());
			activeUtil=this.activeUtility;
			lastActiveBlock=(Block)activeUtil;
			lblCurrentNumberOf.setVisible(false);
			
			if(activeUtility.getCOwner()==null){
				cOwnerLbl = new JLabel("The Bank");
			}else{
				cOwnerLbl = new JLabel(activeUtility.getCOwner().getPlayerName());
			}
			
			updateOwnerLbl();
			updateMoneyLbl();
			
			if(activeUtil.getIsMortgaged()==false){
				if(activeUtil.getCOwner()==p){
					mortgageBtn.setVisible(true);
					unMortgageBtn.setVisible(false);
				}else{
					mortgageBtn.setVisible(false);
					unMortgageBtn.setVisible(false);
				}
				
				largeDeedSelected = new JLabel(largeDeedIcon);
			}else{
				if(activeUtil.getCOwner()==p){
					unMortgageBtn.setVisible(true);
					mortgageBtn.setVisible(false);
				}else{
					mortgageBtn.setVisible(false);
					unMortgageBtn.setVisible(false);
				}
				largeDeedSelected = new JLabel(largeMortgageIcon);
			}
			
			updateLargeDeedSelectedLbl();
		}
	}
	
	private class DisplayLargePropDeed implements ActionListener {
		PropertyBlock activeProperty;
		
		public DisplayLargePropDeed(PropertyBlock prop){
			this.activeProperty = prop;
		}
		
		public void actionPerformed(ActionEvent e){
			setButtonsAndLabelsInvisible();
			
			if(largeDeedSelected!=null){
				largeDeedSelected.setVisible(false);
				contentPane.remove(largeDeedSelected);
			}			

			largeDeedIcon = new ImageIcon(activeProperty.getPropertyDeedLarge());
			largeMortgageIcon = new ImageIcon(activeProperty.getPropertyDeedMortgaged());
			activeProp=activeProperty;
			lastActiveBlock=(Block)activeProp;
			
			lblCurrentNumberOf.setVisible(true);
			
			if(activeProp.getCOwner()==null){
				cOwnerLbl = new JLabel("The Bank");
			}else{
				cOwnerLbl = new JLabel(activeProp.getCOwner().getPlayerName());
			}
			
			updateOwnerLbl();
			updateMoneyLbl();
			updateHouseLbl();
			
			if(activeProp.getIsMortgaged()==false){
				if(activeProp.getCOwner()==p){
					buildHouseBtn.setVisible(true);
					sellHouseBtn.setVisible(true);
					mortgageBtn.setVisible(true);
					unMortgageBtn.setVisible(false);
				}else{
					buildHouseBtn.setVisible(false);
					sellHouseBtn.setVisible(false);
					mortgageBtn.setVisible(false);
					unMortgageBtn.setVisible(false);
				}
				largeDeedSelected = new JLabel(largeDeedIcon);
			}else{
				if(activeProp.getCOwner()==p){
					buildHouseBtn.setVisible(false);
					sellHouseBtn.setVisible(false);
					mortgageBtn.setVisible(false);
					unMortgageBtn.setVisible(true);
				}else{
					buildHouseBtn.setVisible(false);
					sellHouseBtn.setVisible(false);
					mortgageBtn.setVisible(false);
					unMortgageBtn.setVisible(false);
				}
				largeDeedSelected = new JLabel(largeMortgageIcon);
			}
			updateLargeDeedSelectedLbl();
		}
	}
}

